package com.dongdongshop.service.impl;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.admin.model.TbContent;
import com.dongdongshop.admin.model.TbContentCategory;
import com.dongdongshop.admin.model.TbContentCategoryExample;
import com.dongdongshop.admin.model.TbContentExample;
import com.dongdongshop.admin.vo.ContentCategoryVO;
import com.dongdongshop.admin.vo.ContentVO;
import com.dongdongshop.api.GoodsServiceApi;
import com.dongdongshop.mapper.TbContentCategoryMapper;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.service.ContentService;
import com.dongdongshop.util.RedisUtils;
import lombok.var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private TbContentMapper tbContentMapper;
    @Autowired
    private TbContentCategoryMapper categoryMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public List<ContentVO> getAll() {
        List<TbContent> tbContents = tbContentMapper.selectByExample(null);
        List<TbContentCategory> tbContentCategories = categoryMapper.selectByExample(null);
        List<ContentCategoryVO> collect1 = tbContentCategories.stream().map(c -> {
            ContentCategoryVO vo = new ContentCategoryVO();
            BeanUtils.copyProperties(c, vo);
            return vo;
        }).collect(Collectors.toList());
        List<ContentVO> collect = tbContents.stream().map(i -> {
            ContentVO vo = new ContentVO();
            BeanUtils.copyProperties(i, vo);
            vo.setCategoryVOList(collect1);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void insert(ContentVO contentVO) {
        TbContent content = new TbContent();
        BeanUtils.copyProperties(contentVO, content);
        tbContentMapper.insert(content);
    }

    @Override
    public void updateContent(ContentVO conVO) {
        TbContent tbContent = new TbContent();
        BeanUtils.copyProperties(conVO, tbContent);
        Long id = tbContent.getId();
        stringRedisTemplate.delete("contentList"+id);
        tbContentMapper.updateByPrimaryKeySelective(tbContent);
        stringRedisTemplate.delete("contentList"+id);
    }

    @Override
    public void deleteContent(Long[] ids) {
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        tbContentMapper.deleteByExample(example);
    }

    @Override
    public List<ContentVO> contentList(Long id) {
        if (stringRedisTemplate.hasKey("contentList"+id)){
            String s = stringRedisTemplate.opsForValue().get("contentList" + id);
            List<ContentVO> tbContents = JSON.parseArray(s, ContentVO.class);
            /*List<ContentVO> collect = tbContents.stream().map(s1 -> {
                    ContentVO vo = new ContentVO();
                     BeanUtils.copyProperties(s1, vo);
                    return vo;
                }).collect(Collectors.toList());*/
                return tbContents;
        }else {
            TbContentExample example = new TbContentExample();
            TbContentExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(id);
            List<TbContent> tbContents = tbContentMapper.selectByExample(example);
            List<ContentVO> collect = tbContents.stream().map(i -> {
                ContentVO vo = new ContentVO();
                BeanUtils.copyProperties(i, vo);
                return vo;
            }).collect(Collectors.toList());
            String s = JSONObject.toJSONString(collect);
            stringRedisTemplate.opsForValue().set("contentList"+id,s);
            return collect;
        }

    }

    @Override
    public List<ContentVO> selectByIdLunBo(Long categoryId) {
        Boolean aBoolean = stringRedisTemplate.hasKey(RedisUtils.SLIDE_KEY);
        if (aBoolean) {
            //从redis里取
            String s = stringRedisTemplate.opsForValue().get(RedisUtils.SLIDE_KEY);
            List<ContentVO> tbContents = JSON.parseArray(s, ContentVO.class);
            return tbContents;
        }else {
            //从数据库取
            TbContentExample example = new TbContentExample();
            TbContentExample.Criteria criteria = example.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);
            List<TbContent> tbContents = tbContentMapper.selectByExample(example);
            List<ContentVO> collect = tbContents.stream().map(m -> {
                ContentVO vo = new ContentVO();
                BeanUtils.copyProperties(m, vo);
                return vo;
            }).collect(Collectors.toList());
            String s = JSONObject.toJSONString(collect);
            stringRedisTemplate.opsForValue().set(RedisUtils.SLIDE_KEY,s);
            return collect;
        }
    }

    @Override
    public List<ContentVO> selectByIdNew(Long categoryId) {
        Boolean aBoolean = stringRedisTemplate.hasKey(RedisUtils.DAILY_KEY);
        if (aBoolean) {
            //从redis里取
            String s = stringRedisTemplate.opsForValue().get(RedisUtils.DAILY_KEY);
            List<ContentVO> tbContents = JSON.parseArray(s, ContentVO.class);
            return tbContents;
        }else {
            //从数据库取
            TbContentExample example = new TbContentExample();
            TbContentExample.Criteria criteria = example.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);
            List<TbContent> tbContents = tbContentMapper.selectByExample(example);
            List<ContentVO> collect = tbContents.stream().map(m -> {
                ContentVO vo = new ContentVO();
                BeanUtils.copyProperties(m, vo);
                return vo;
            }).collect(Collectors.toList());
            String s = JSONObject.toJSONString(collect);
            stringRedisTemplate.opsForValue().set(RedisUtils.DAILY_KEY,s);
            return collect;
        }
    }

    @Override
    public List<ContentVO> selectByIdLove(Long categoryId) {
        Boolean aBoolean = stringRedisTemplate.hasKey(RedisUtils.LOVE_KEY);
        if (aBoolean) {
            //从redis里取
            String s = stringRedisTemplate.opsForValue().get(RedisUtils.LOVE_KEY);
            List<ContentVO> tbContents = JSON.parseArray(s, ContentVO.class);
            return tbContents;
        }else {
            //从数据库取
            TbContentExample example = new TbContentExample();
            TbContentExample.Criteria criteria = example.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);
            List<TbContent> tbContents = tbContentMapper.selectByExample(example);

            List<ContentVO> collect = tbContents.stream().map(m -> {
                ContentVO vo = new ContentVO();
                BeanUtils.copyProperties(m, vo);
                return vo;
            }).collect(Collectors.toList());
            String s = JSONObject.toJSONString(collect);
            stringRedisTemplate.opsForValue().set(RedisUtils.LOVE_KEY,s);
            return collect;
        }
    }

}
